package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TMember;
import com.quwan.entity.TMemberReport;
import com.quwan.manager.service.MemberReportService;
import com.quwan.mapper.TMemberMapper;
import com.quwan.mapper.TMemberReportMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author laozhang
 * @date 2021/11/18 9:36
 */
@Service
@RequiredArgsConstructor
public class MemberReportServiceImpl implements MemberReportService {
    private final TMemberReportMapper memberReportMapper;
    private final TMemberMapper memberMapper;
    private final OssConfig ossConfig;
    @Override
    public PageResult<TMemberReport> getMemberReportListPage(int page, int size, Integer status, String name, String beginTime, String endTime, String sourceId, String targetId) {
        LambdaQueryWrapperX<TMemberReport> wrapper = new LambdaQueryWrapperX<TMemberReport>()
                .orderByDesc(TMemberReport::getAddTime);
        if(StringUtils.isNotBlank(name)) {
            wrapper.like(TMemberReport::getSourceName,name);
        }
        if(status !=null) {
            wrapper.eq(TMemberReport::getStatus,status);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberReport::getAddTime,beginTime);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberReport::getAddTime,endTime+" 23:59:59");
        }
        if(StringUtils.isNotBlank(sourceId)){
            wrapper.eq(TMemberReport::getSourceId, sourceId);
        }
        if(StringUtils.isNotBlank(targetId)){
            wrapper.eq(TMemberReport::getTargetId, targetId);
        }

        Page<TMemberReport> memberReportPage = memberReportMapper.selectPage(new Page<>(page, size), wrapper);
        List<TMemberReport> reslist = memberReportPage.getRecords();
        /* 开始遍历 */
        for (TMemberReport ban : reslist) {
            if(ban.getImg()!= null && ban.getImg().length()>0){
                ban.setImg(ossConfig.getHostname()+"/"+ban.getImg());
            }
        }
        PageResult<TMemberReport> result = new PageResult<TMemberReport>()
                .setCurrent(memberReportPage.getCurrent())
                .setPages(memberReportPage.getPages())
                .setSize(memberReportPage.getSize())
                .setTotal(memberReportPage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<TMemberReport> getMemberReportList(Integer status, String name, String beginTime, String endTime) {
        LambdaQueryWrapperX<TMemberReport> wrapper = new LambdaQueryWrapperX<TMemberReport>()
                .orderByDesc(TMemberReport::getAddTime);
        if(StringUtils.isNotBlank(name)) {
            wrapper.like(TMemberReport::getSourceName,name);
        }
        if(status !=null) {
            wrapper.eq(TMemberReport::getStatus,status);
        }
        if(StringUtils.isNotBlank(beginTime)) {
            wrapper.ge(TMemberReport::getAddTime,beginTime);
        }
        if(StringUtils.isNotBlank(endTime)) {
            wrapper.le(TMemberReport::getAddTime,endTime+" 23:59:59");
        }
        List<TMemberReport> reslist = memberReportMapper.selectList(wrapper);
        /* 开始遍历 */
        for (TMemberReport ban : reslist) {
            if(ban.getImg()!= null && ban.getImg().length()>0){
                ban.setImg(ossConfig.getHostname()+"/"+ban.getImg());
            }
        }
        return reslist;
    }

    @Override
    public boolean updateMemberReportStatus(Long id,Long targetId,Integer status) {
        if(status.equals(1)){
            memberMapper.update(null,
                    new LambdaUpdateWrapper<TMember>()
                            .set(TMember::getStatus, 0)
                            .eq(TMember::getId, targetId));
        }
        return  memberReportMapper.update(null,
                new LambdaUpdateWrapper<TMemberReport>()
                        .set(TMemberReport::getStatus, status)
                        .eq(TMemberReport::getId, id)) == 1;
    }
}
